﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Windows;
using BDSync.Domains.DBConn;
using BDSync.Domains.DBSnapShot;
using Microsoft.Extensions.DependencyInjection;
using MSSQL.DBSync.UI.Components.MainChoice;
using MSSQL.DBSync.UI.Components.MainWindow;
using MSSQL.DBSync.UI.Components.MakeComparsion;
using MSSQL.DBSync.UI.Components.MakeSnapshot;
using MSSQL.DBSync.UI.Components.ManageConn;
using Serilog;
using Serilog.Exceptions;
using Serilog.Formatting.Json;



namespace MSSQL.DBSync.UI
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        
        public App()
        {
            InitializeComponent();
            Bootstrap();
            Log.Information("App Start");
        }

        
        private void Bootstrap()
        {

            //Log.Logger = new LoggerConfiguration()
            //    .MinimumLevel.Debug()
            //    .WriteTo
            //    .File( @"logs\app-log.txt",rollingInterval:RollingInterval.Day)
            //    .CreateLogger();

            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .Enrich.WithExceptionDetails()
                .WriteTo.File(
                    new JsonFormatter(renderMessage: true),
                    @"logs\app-log.txt",
                    rollingInterval: RollingInterval.Day
                )
                .CreateLogger();

            //初始化容器
            var serviceCollection = new ServiceCollection();
            ConfigureServices(serviceCollection);
            mServiceProvider = serviceCollection.BuildServiceProvider();

        }

        private static ServiceProvider mServiceProvider;
        public static ServiceProvider GetContainer()
        {
            return mServiceProvider;
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            //XamlDisplay.Init();
            base.OnStartup(e);

            //启动窗体
            var main = mServiceProvider.GetRequiredService<IMainWindow>();
            main.Show();
        }


        /// <summary>
        /// 对象注册
        /// </summary>
        /// <param name="services"></param>
        private void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<IMainWindowVM, MainWindowVM>();
            services.AddSingleton<IMainWindow,MainWindow>();


            services.AddSingleton<DBConnRepo>();
            services.AddSingleton<IDBConnService, DBConnService>(prd =>
            {
                var rpo = prd.GetService<DBConnRepo>();
                return new DBConnService(rpo);
            });


            services.AddSingleton<IDBSnapShotServices, DBSnapShotServices>();
            services.AddSingleton<IMakeSnapshotVM, MakeSnapshotVM>((svcPrd ) =>
            { 
                var depsvc = svcPrd.GetService<IDBSnapShotServices>();
                var conSvc = svcPrd.GetService<IDBConnService>();
                return new MakeSnapshotVM(depsvc, conSvc);
            });

            services.AddSingleton<IMakeSnapshotView, MakeSnapshotView>();
            services.AddSingleton<IDBCompareServices, DBCompareServices>();

            services.AddSingleton<IMakeComparsionVM, MakeComparsionVM>((svcPrd) =>
            {
                var depsvc = svcPrd.GetService<IDBCompareServices>();
                var conSvc = svcPrd.GetService<IDBConnService>();
                var msgwd = svcPrd.GetService<IMsgWindow>();
                return new MakeComparsionVM(depsvc, conSvc, msgwd);
            });
            services.AddSingleton<IMakeComparsionView, MakeComparsionView>();

            services.AddSingleton<IMainChoiceVM, MainChoiceVM>();
            services.AddSingleton<IMainChoiceView, MainChoiceView>();

            services.AddSingleton<IMsgWindow, MsgWindow>();

            services.AddSingleton<IManageConnVM, ManageConnVM>((svcPrd) =>
            {
                var svc = svcPrd.GetService<IDBConnService>();
                return new ManageConnVM(svc);
            });
            services.AddSingleton<IManageConnView, ManageConnView>();



        }
    }
}
